import random
import time

from sqlalchemy import and_

from exts import db
from flask import Blueprint, request, Response, jsonify, render_template, url_for, redirect, session
import json

from user.model import User

user = Blueprint('user', __name__);

# 学生列表
@user.route('user_list', methods=['GET', 'POST'])
def user_list():
    user_list = User.query.all()
    return render_template('user_list.html', user_list=user_list)

# 根据名称搜索
@user.route('/user/search', methods=['GET', 'POST'])
def search():
    keyword = request.args.get('keyword')
    user_list = User.query.filter(
        User.username.like('%{keyword}%'.format(keyword=keyword))
    ).all()
    return render_template('user_list.html', user_list=user_list)

# 删除学生
@user.route('/user/remove', methods=['GET', 'POST'])
def remove():
    id = request.args.get('id')
    user = User.query.filter(
        User.id == id
    ).first()
    db.session.delete(user)
    db.session.commit()
    return redirect(url_for('user.user_list'))



# 查看，更新，添加学生
@user.route('/user/edit', methods=['GET', 'POST'])
def edit():
    # 如果是get跳转详情页面，否则添加
    if request.method=='GET':
        id = request.args.get('id')
        option = request.args.get('option')
        user = User.query.filter(
            User.id == id
        ).first()
        if option == 'detail':
            return render_template('user_editor.html', user=user)
        else:
            return render_template('user_editor.html', user=user, update=True)
    else:
        id = request.form.get('id')
        username = request.form.get('username')
        password = request.form.get('password')
        student = User.query.filter(
            User.id == id
        ).first()
        # 如果存在用户，那么更新，不存在添加
        if student is None:
            student = User(username=username, password=password)
            db.session.add(student)
            db.session.commit()
        else:
            student.username = username
            student.password = password
            db.session.commit()
        return redirect(url_for("user.user_list"))

@user.route('/logout', methods=['GET', 'POST'])
def logout():
    session.clear()
    return redirect("/")

# 登录
@user.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    else:
        username = request.form.get('username')
        password = request.form.get('password')
        # select user
        user = User.query.filter(
            User.username == username,
            User.password == password
        ).first()
        # 封装格式

        if (user is not None):
            # 保存用户信息
            session['user_id'] = user.id
            session['is_login'] = True
            session['user_name'] = username
            return render_template('admin.html')
        else:
            return render_template('login.html', msg='账号或密码错误！')



# 注册
@user.route('/register', methods=['GET', 'POST'])
def register():
    if request.method=='GET':
        return render_template('register.html')
    else:
        username = request.form.get('username')
        password = request.form.get('password')
        # select user
        user = User.query.filter(User.username == username).first()
        if (user is not None):
            return render_template('register.html', msg='用户已存在！')
        user = User(username=username, password=password)
        db.session.add(user)
        db.session.commit()
        return render_template('login.html')

# 用户信息
@user.route('/userinfo', methods=['GET', 'POST'])
def userinfo():
    user_id = session.get('user_id')
    user = User.query.filter(
        User.id == user_id
    ).first()
    return render_template('userinfo.html', user=user)

# 更新
@user.route('/update', methods=['GET', 'POST'])
def update():
    user_id = session.get('user_id')
    username = request.form.get('username')
    password = request.form.get('password')
    user = User.query.filter(
        User.id == user_id
    ).first()
    user.username  = username
    user.password = password
    db.session.commit()
    return render_template('userinfo.html', user=user, msg='更新成功!')

# 首页
@user.route('/first_page', methods=['GET', 'POST'])
def first_page():
    return render_template('first_page.html')